﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1.DesignForms
{
    public partial class CreateEntityForm : Form
    {
        public CommonEntity CreatedEntity { get; set; }
        public KnowlgeMap SelectMap { get; internal set; }
        public string SelectGroup { get; internal set; }

        private Dictionary<string, string> propertities = new Dictionary<string, string>();
        public CreateEntityForm()
        {
            InitializeComponent();
        }
        private void CreateEntityForm_Load(object sender, EventArgs e)
        {
            if (CreatedEntity != null)
            {
                this.textBox1.Text = CreatedEntity.id;
                this.textBox2.Text = CreatedEntity.labels[0];
                this.comboBox1.Text = CreatedEntity.GetGroup();
                foreach (var key in CreatedEntity.properties.Keys)
                {
                    var lvi = new ListViewItem()
                    {
                        Text = key
                    };
                    lvi.SubItems.Add(CreatedEntity.properties[key]);
                    this.listView1.Items.Add(lvi);
                }
                propertities = CreatedEntity.properties;
            }
            else
            {
                this.textBox1.Text = Guid.NewGuid().ToString();
                this.comboBox1.Text = SelectGroup;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CreatedEntity != null)
            {
                CreatedEntity.id = this.textBox1.Text;
                CreatedEntity.labels.Clear();
                CreatedEntity.SetGroup(this.comboBox1.Text);

            }
            else
            {
                CreatedEntity = new CommonEntity()
                {
                    id = this.textBox1.Text
                };
                CreatedEntity.labels.Add(this.textBox2.Text);
                CreatedEntity.properties = propertities;
            }
            CreatedEntity.labels.Add(this.textBox2.Text);
            CreatedEntity.SetGroup(this.comboBox1.Text);
            propertities["name"] = this.textBox2.Text;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var lvi = new ListViewItem()
            {
                Text = this.textBox3.Text
            };
            lvi.SubItems.Add(this.textBox4.Text);
            propertities.Add(this.textBox3.Text, this.textBox4.Text);
            this.listView1.Items.Add(lvi);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var groups = SelectMap.GetGroups();
            this.comboBox1.Items.AddRange(groups.ToArray());
            if (this.SelectGroup != null)
            {
                this.comboBox1.Text = this.SelectGroup;
            }
            else if (CreatedEntity != null)
            {
                this.comboBox1.Text = CreatedEntity.GetGroup();
            }
        }
    }
}
